package com.slipper.util;

import com.slipper.core.gson.GsonUtil;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

public class LicenseUtil {
    public static String createProbation(String machineCode) throws Exception {
        long licenseTime = Objects.requireNonNull(DateUtil.parseDate(DateUtil.formatDate(new Date()), "yyyy-MM-dd")).getTime();

        Map<String, Object> dataMap = GsonUtil.toMap(AESUtil.decrypt(machineCode));
        if (dataMap == null) return null;

        dataMap.put("machineCode", machineCode);
        dataMap.put("licenseVersion", "试用版");
        dataMap.put("licenseTime", licenseTime);
        dataMap.put("expireTime", DateUtil.getNextMonth(licenseTime, 6).getTime() - 1000);

        return AESUtil.encrypt(GsonUtil.toJson(dataMap));
    }

    public static String createLicense(String machineCode) throws Exception {
        long licenseTime = Objects.requireNonNull(DateUtil.parseDate(DateUtil.formatDate(new Date()), "yyyy-MM-dd")).getTime();

        Map<String, Object> dataMap = GsonUtil.toMap(AESUtil.decrypt(machineCode));
        if (dataMap == null) return null;

        dataMap.put("machineCode", machineCode);
        dataMap.put("licenseVersion", "正式版");
        dataMap.put("licenseTime", licenseTime);
        dataMap.put("expireTime", DateUtil.getNextDay(DateUtil.getNextYear(licenseTime, 100), 1).getTime() - 1000);

        return AESUtil.encrypt(GsonUtil.toJson(dataMap));
    }

    public static Map<String, Object> parseLicense(String license) {
        return GsonUtil.toMap(AESUtil.decrypt(license));
    }

    public static void main(String[] args) throws Exception {
        System.out.println(createLicense("6YeSr9rXNt14BDn0c90fd9f65a04872cnqxsmDHbZOVus2Lq6e7bde19d9e16d98BOVdaJslPlbU6eI9PDPeJEZ3azx8NUmuWPeNuxCq39JUfAK5NrRO+x4C7JyLmnlJpjFOQ7vz54xYvFA1mExdhMvN62TN8oZpE5R7mp1pwD8="));
        System.out.println(createProbation("ynWxZVyMUhp9cDYpXDfrYPnH5jBfpxQ4e3d10e7989316c1de0d07a1546bf7156snVuJ9o0HeD+UWS4+zGmb2xXweBG5tlOncyk+KZWuD5qZ8BcEejTA1R700poLSkWLXAi0okIABEWfyOcDZqcz1uyYceq7BAb/fAyDpP3zMc4naIVPbuTdr+GePpoSU+Kh6L8iS205/nw8BnicZhW08ofzCe+dcsHGXKZIoAbfY4="));
    }
}
